package com.changgou.system;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author kfzhao
 * @version 1.0
 * @className GateWayApplication
 * @description
 * @date 2021/12/25 14:21
 */
@SpringBootApplication
@EnableEurekaClient
public class GateWayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GateWayApplication.class, args);
    }

    @Bean
    public KeyResolver ipKeyResolver() {
        return new KeyResolver() {
            /**
             * 获取当前访问的客户端ip，并做一些限制
             * @param exchange
             * @return reactor.core.publisher.Mono<java.lang.String>
             * @throws
             * @description
             * @author kfzhao
             * @date 2021/12/25 14:55
             */
            @Override
            public Mono<String> resolve(ServerWebExchange exchange) {
                // just()方法进行访问控制限制 这里限流ip
                return Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
            }
        };
    }
}
